package com.jingshui.Controller;

import com.alibaba.fastjson.JSONObject;
import com.jingshui.Entity.User;
import com.jingshui.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import orm.base.Ex.BaseControllerEx;
import orm.utils.WxUtils;

import java.net.URLEncoder;
import java.util.Map;

/**
 * 用户Controller
 * Created by ashin on 2017/9/28.
 */
@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserController extends BaseControllerEx {

    @Autowired
    UserService userService;

    @Override
    protected void init() {
        super.init(userService);
    }

    @Autowired
    WxUtils wxUtils;

    /**
     * 通过微信发起授权
     *
     * @return
     */
    @RequestMapping(value = "/oauthLogin", method = RequestMethod.GET)
    public String oauthLogin() throws Exception {
        String url = wxUtils.redirectWxOauth(URLEncoder.encode("http://jsq.0511mzone.com/user/doLogin", "utf-8"));
        return "redirect:" + url;
    }

    /**
     * 微信跳转首页
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doLogin", method = RequestMethod.GET)
    public String doLogin(@RequestParam Map map) throws Exception {
        return userService.getOpenIdWxOauth(map, session,"/user/index");
    }

    /**
     * 首页
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() throws Exception {
        return "/new/index";
    }

    /**
     * 产品
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/adv", method = RequestMethod.GET)
    public String adv() throws Exception {
        return "/new/adv";
    }

    /**
     * 我的
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/mine", method = RequestMethod.GET)
    public String mine() throws Exception {
        return "/new/mine";
    }

    /**
     * 通过微信发起授权 - 预定
     *
     * @return
     */
    @RequestMapping(value = "/oauthReserve", method = RequestMethod.GET)
    public String oauthReserve() throws Exception {
        String url = wxUtils.redirectWxOauth(URLEncoder.encode("http://jsq.0511mzone.com/user/reserve?from=wx", "utf-8"));
        return "redirect:" + url;
    }

    /**
     * 预定
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/reserve", method = RequestMethod.GET)
    public String reserve(@RequestParam Map map) throws Exception {
        if(map.get("from") != null && "wx".equals(map.get("from")))
            userService.getOpenIdWxOauth(map,session,"/new/recommend");
        return "/new/reserve";
    }

    /**
     * 通过微信发起授权 - 预定
     *
     * @return
     */
    @RequestMapping(value = "/oauthDetail_1", method = RequestMethod.GET)
    public String oauthDetail_1() throws Exception {
        String url = wxUtils.redirectWxOauth(URLEncoder.encode("http://jsq.0511mzone.com/user/detail?id=1&from=wx", "utf-8"));
        return "redirect:" + url;
    }

    /**
     * 通过微信发起授权 - 预定
     *
     * @return
     */
    @RequestMapping(value = "/oauthDetail_2", method = RequestMethod.GET)
    public String oauthDetail_2() throws Exception {
        String url = wxUtils.redirectWxOauth(URLEncoder.encode("http://jsq.0511mzone.com/user/detail?id=2&from=wx", "utf-8"));
        return "redirect:" + url;
    }

    /**
     * 通过微信发起授权 - 预定
     *
     * @return
     */
    @RequestMapping(value = "/oauthDetail_3", method = RequestMethod.GET)
    public String oauthDetail_3() throws Exception {
        String url = wxUtils.redirectWxOauth(URLEncoder.encode("http://jsq.0511mzone.com/user/detail?id=3&from=wx", "utf-8"));
        return "redirect:" + url;
    }

    /**
     * 产品详情
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(@RequestParam Map map) throws Exception {
        if(map.get("from") != null && "wx".equals(map.get("from")))
            userService.getOpenIdWxOauth(map,session,"/new/recommend");
        return "/new/detail";
    }

    /**
     * 用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/user_info", method = RequestMethod.GET)
    public String user_info() throws Exception {
        return "/new/user-info";
    }

    /**
     * 通过微信发起授权 - 推荐
     *
     * @return
     */
    @RequestMapping(value = "/oauthRecommend", method = RequestMethod.GET)
    public String oauthRecommend() throws Exception {
        String url = wxUtils.redirectWxOauth(URLEncoder.encode("http://jsq.0511mzone.com/user/recommend?from=wx", "utf-8"));
        return "redirect:" + url;
    }

    /**
     * 推荐
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/recommend", method = RequestMethod.GET)
    public String recommend(@RequestParam Map map) throws Exception {
        if(map.get("from") != null && "wx".equals(map.get("from")))
            userService.getOpenIdWxOauth(map,session,"/new/recommend");
        return "/new/recommend";
    }

    /**
     * 推荐人列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/recommendList", method = RequestMethod.GET)
    public String recommendList() throws Exception {
        return "/new/recommend-list";
    }

    /**
     * 租赁信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/rent", method = RequestMethod.GET)
    public String rent() throws Exception {
        return "/new/rent";
    }

    /**
     * 获得用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doGetUserInfo", method = RequestMethod.POST)
    public JSONObject doGetUserInfo() throws Exception {
        return userService.getUserInfo(session);
    }

    /**
     * 从session中获取User
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doGetSessionUser", method = RequestMethod.POST)
    public JSONObject doGetSessionUser() throws Exception {
        return this.codeData((User)session.getAttribute("user"));
    }

    /**
     * 从session中获取User
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doCheckPassword", method = RequestMethod.POST)
    public JSONObject doCheckPassword(@RequestParam Map map) throws Exception {
        return userService.checkPassword(map,session);
    }

    /**
     * 租赁信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/manage", method = RequestMethod.GET)
    public String manage() throws Exception {
        return "/new/manage";
    }

    /**
     * 租赁信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/manage_info", method = RequestMethod.GET)
    public String manage_info() throws Exception {
        return "/new/manage-info";
    }

    /**
     * 更新用户信息
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    protected JSONObject doUpdate(@RequestParam Map map) throws Exception {
        User user = (User) session.getAttribute("user");
        map.put("openId", user.getOpenId());
//        map.put("openId", "otT_v1UJqMlsKlxe-gmVAGUNfQA4");
        return super.doUpdate(map);
    }


}
